package com.zmj.framework.handler.exception;

import com.zmj.framework.exception.client.*;
import com.zmj.framework.response.ResponseCode;
import com.zmj.framework.response.Result;
import feign.FeignException;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ClientExceptionControllerAdvice {

    @ExceptionHandler(AccountDisabledException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleAccountDisabledException(AccountDisabledException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.ACCOUNT_DISABLED);
    }

    @ExceptionHandler(AccountExpiredException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleAccountExpiredException(AccountExpiredException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.ACCOUNT_EXPIRED);
    }

    @ExceptionHandler(AccountLockedException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleAccountLockedException(AccountLockedException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.ACCOUNT_LOCKED);
    }

    @ExceptionHandler(BadCredentialsException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleBadCredentialsException(BadCredentialsException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.BAD_CREDENTIALS);
    }

    @ExceptionHandler(BadParametersException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleBadParametersException(BadParametersException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.REQUEST_PARAMETERS_ERROR);
    }

    @ExceptionHandler(BadRequestException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleBadRequestException(BadRequestException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.BAD_REQUEST);
    }
    @ExceptionHandler(ClientErrorException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleClientErrorException(ClientErrorException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.BAD_REQUEST);
    }
    @ExceptionHandler(FirstLoginException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleFirstLoginException(FirstLoginException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.ACCOUNT_FIRST_LOGIN);
    }

    @ExceptionHandler(NoneClientException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleNoneClientException(NoneClientException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.CLIENT_NOT_FOUND);
    }

    @ExceptionHandler(NoneUserException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleNoneUserException(NoneUserException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.ACCOUNT_NOT_FOUND);
    }

    @ExceptionHandler(NotFoundException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleNotFoundException(NotFoundException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.NOT_FOUND);
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleUnauthorizedException(UnauthorizedException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.UNAUTHORIZED);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleValidationException(MethodArgumentNotValidException e) {
        String errorMessage = e.getBindingResult()
                .getAllErrors()
                .stream()
                .map(input -> {
                    if (input instanceof FieldError) {
                        FieldError fieldError = (FieldError) input;
                        String field = fieldError.getField();
                        return field + input.getDefaultMessage();
                    }
                    return input.getDefaultMessage();
                })
                .collect(Collectors.joining(","));
        BadParametersException badParametersException = new BadParametersException(errorMessage);
        return handleBadParametersException(badParametersException);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        String errorMessage = e.getParameterName() + e.getMessage();
        BadParametersException badParametersException = new BadParametersException(errorMessage);
        return handleBadParametersException(badParametersException);
    }

    @ExceptionHandler(MissingPathVariableException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleMissingPathVariableException(MissingPathVariableException e) {
        String errorMessage = e.getVariableName() + e.getMessage();
        BadParametersException badParametersException = new BadParametersException(errorMessage);
        return handleBadParametersException(badParametersException);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleConstraintViolationException(ConstraintViolationException e) {
        String errorMessage = e.getConstraintViolations()
                .stream()
                .map(input -> {
                    String message = input.getPropertyPath() + input.getMessage();
                    if (input.getPropertyPath() instanceof PathImpl) {
                        PathImpl propertyPath = (PathImpl) input.getPropertyPath();
                        message = propertyPath.getLeafNode() + input.getMessage();
                    }
                    return message;
                })
                .collect(Collectors.joining(","));
        BadParametersException badParametersException = new BadParametersException(errorMessage);
        return handleBadParametersException(badParametersException);
    }
}
